package hp.com.servlet.UsersServlet;

import hp.com.entity.Users;
import hp.com.service.UsersService;
import hp.com.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/checkPwdUpdateServlet")
public class CheckPwdUpdateServlet extends HttpServlet {
    private UsersService usersService = new UsersServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            int id = Integer.parseInt(req.getParameter("id"));
            String oldpwd = req.getParameter("oldpwd");
            String newpwd1 = req.getParameter("newpwd1");
            String newpwd2 = req.getParameter("newpwd2");
            if (newpwd1.equals(newpwd2)){
                Users oldUsers = new Users();
                oldUsers.setId(id);
                oldUsers.setPwd(oldpwd);
                boolean b = usersService.checkOldPwd(oldUsers);
                if (b){
                    Users users = new Users();
                    users.setId(id);
                    users.setPwd(newpwd1);
                    boolean b1 = usersService.updateUsersPwd(users);
                    if (b1){
                        req.setAttribute("pwdmsg","修改成功！");
                    }else {
                        req.setAttribute("pwdmsg","修改失败！");
                    }
                }else{
                    req.setAttribute("pwdmsg","密码输入错误，请重新输入！");
                }
            }else {
                req.setAttribute("pwdmsg","两次密码输入不同，请重新输入！");
            }
            req.getRequestDispatcher("easybuy/Member_Safe.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
